#!/bin/bash

data_url="git@gitee.com:mdsSJY/address-db.git"
fc_data="./address-db"
update_sh="./start.sh"

cd /usr/sjy || { echo "cd failed"; exit 1; }

# 1.定义数据路径
if [ -d "$fc_data" ]; then
    rm -r "$fc_data"
fi
#mkdir -p "$fc_data" || { echo "Failed to create directory $fc_data."; exit 1; }

# 2.移动至数据路径
#cd "$fc_data" || { echo "cd failed"; exit 1; }

# 3.拉取gitee公钥
ssh-keyscan -t rsa,ecdsa,ed25519 gitee.com >> ~/.ssh/known_hosts 2>/dev/null

# 3.git拉取
git clone "$data_url" || { echo "git clone failed. $data_url"; exit 1; }

# 4.进入拉取的文件中
cd "$fc_data" || { echo "cd failed"; exit 1; }

chmod +x "$update_sh"

if [ ! -f "$update_sh" ] || [ ! -x "$update_sh" ]; then
    echo "Error: File '$update_sh' does not exist or is not executable."
    exit 1
fi

# 执行shell
if ! "$update_sh" ; then
    exit 1
fi